<template>
  <el-dialog
    :title="!dataForm.brandId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="收货人姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="收货人姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="省/城/区" prop="province">
        <v-distpicker @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea"
        :province="dataForm.province" :city="dataForm.city" :area="dataForm.region"></v-distpicker>
      </el-form-item>
      <el-form-item label="详细地址" prop="detailAddress">
        <el-input v-model="dataForm.detailAddress" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model="dataForm.tag" placeholder="标签"></el-input>
      </el-form-item>
      <el-form-item label="是否默认" prop="defaultStatus">
        <el-switch
          v-model="dataForm.defaultStatus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { addAddress, infoAddress, updateAddress } from '@/api/member/member'
import VDistpicker from "v-distpicker";
import {getToken} from "@/utils/login-token";

export default {
  components: {
    VDistpicker
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        memberId: '',
        name: '',
        phone: '',
        province: '',
        city: '',
        region: '',
        detailAddress: '',
        tag: '',
        defaultStatus: 1,
      },
      dataRule: {
        name: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('收货人姓名不能为空'))
              } else if (value.length > 10) {
                callback(new Error('收货人姓名不能超过10个字'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
        phone: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('电话号码不能为空'))
              } else {
                const reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
                if(value.length === 11){
                  if(reg.test(value)){
                    callback()
                  }else {
                    callback(new Error('电话号码格式不正确'))
                  }
                }else {
                  callback(new Error('电话号码格式不正确'))
                }
              }
            }, trigger: 'blur' }
        ],
        province: [
          { validator: (rule, value, callback) => {
              if(this.dataForm.province.length > 0){
                callback()
              }else{
                callback(new Error('地址不能为空'))
              }
            }, trigger: 'blur' }
        ],
        detailAddress: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('详细地址不能为空'))
              } else if (value.length > 50) {
                callback(new Error('详细地址不能超过50个字'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
        tag: [
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('标签不能为空'))
              } else if (value.length > 5) {
                callback(new Error('标签不能超过5个字'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    onChangeProvince(e){
      this.dataForm.province = e.value
    },
    onChangeCity(e){
      this.dataForm.city = e.value
    },
    onChangeArea(e){
      this.dataForm.region = e.value
    },
    check() {
      console.log(this.dataForm)
    },
    init(id) {
      this.dataForm = {
        id: 0,
        memberId: '',
        name: '',
        phone: '',
        province: '',
        city: '',
        region: '',
        detailAddress: '',
        tag: '',
        defaultStatus: 1,
      }
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          infoAddress(this.dataForm.id)
            .then(({ data }) => {
              this.dataForm = data
            })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.token = getToken()
          if(!this.dataForm.id){
            addAddress(this.dataForm)
              .then(() => {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('refreshDataList')
              })
          } else {
            updateAddress(this.dataForm)
              .then(() => {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('refreshDataList')
              })
          }
        }
      })
    }
  }
}
</script>
